import myConfig from '@/myConfig'

export function toWrapperStr(value) {
	if (typeof(value) === 'undefined' || value === null) {
		return null
	}
	return JSON.stringify({
		type: typeof value,
		value: value
	})
}

export function fromWrapperStr(wrapperStr) {
	if (wrapperStr === null || wrapperStr === '') {
		return null
	}
	const wrapper = JSON.parse(wrapperStr)
	switch (wrapper.type) {
		case 'number':
			return Number(wrapper.value)
		case 'boolean':
			return Boolean(wrapper.value)
		case 'object':
			return JSON.parse(wrapper.value)
		default:
			return wrapper.value
	}
}

//#ifdef MP-WEIXIN
const appId = wx.getAccountInfoSync().miniProgram.appId
const app = appId
export function getAppId() {
	return appId
}
//#endif
//#ifdef H5
const app = window.location.host
//#endif

function checkStorageVersion() {
	const storageVersionKey = "storageVersion"
	const storageVersion = uni.getStorageSync(storageVersionKey)
	if (!storageVersion) {
		uni.clearStorageSync()
		uni.setStorageSync(storageVersionKey, myConfig.app.storageVersion)
		return
	}
	if (storageVersion !== myConfig.app.storageVersion) {
		uni.clearStorageSync()
		uni.setStorageSync(storageVersionKey, myConfig.app.storageVersion)
		return
	}
}

export function localStorageGet(key) {
	checkStorageVersion()
	return fromWrapperStr(uni.getStorageSync(key))
}

export function localStorageSet(key, val) {
	checkStorageVersion()
	if (val === undefined || val === null) {
		uni.removeStorageSync(key)
	} else {
		uni.setStorageSync(key, toWrapperStr(val))
	}
}

function saveToNet(key, value) {
	localStorageSet(key, value)
}

function getFromNet(key) {
	return localStorageGet(key)
}

const mapModelKey = 'mapModel'

export function getMapModel() {
	return getFromNet(mapModelKey) || 'point'
}

export function setMapModel(mapModel) {
	return saveToNet(mapModelKey, mapModel)
}

const latKey = 'lat'

export function getLat() {
	return localStorageGet(latKey) || 23
}

export function setLat(lat) {
	return localStorageSet(latKey, lat)
}

const lngKey = 'lng'

export function getLng() {
	return localStorageGet(lngKey) || 113
}

export function setLng(lng) {
	return localStorageSet(lngKey, lng)
}

// const TokenKey = 'accessToken'
const TokenKey = 'token'

export function getToken() {
	return localStorageGet(TokenKey)
}

export function setToken(token) {
	localStorageSet(TokenKey, token)
}

const userIdKey = 'userid'

export function getUserId() {
	return localStorageGet(userIdKey)
}

export function setUserId(userId) {
	localStorageSet(userIdKey, userId)
}

const userNameKey = 'userName'

export function getUserName() {
	return localStorageGet(userNameKey)
}

export function setUserName(userName) {
	localStorageSet(userNameKey, userName)
}


const shortCompanyNameKey = 'shortCompanyName'

export function getShortCompanyName() {
	return localStorageGet(shortCompanyNameKey)
}

export function setShortCompanyName(shortCompanyName) {
	localStorageSet(shortCompanyNameKey, shortCompanyName)
}

const languageKey = 'language'

export function getLanguage() {
	return getFromNet(languageKey)
}

export function setLanguage(language) {
	return saveToNet(languageKey, language)
}

const tabbarHeightKey = 'tabbarHeight'

export function getTabbarHeight() {
	return localStorageGet(tabbarHeightKey)
}

export function setTabbarHeight(tabbarHeight) {
	localStorageSet(tabbarHeightKey, tabbarHeight)
}

const themeKey = 'theme'

export function getTheme() {
	return localStorageGet(themeKey) || 'red'
}

export function setTheme(theme) {
	localStorageSet(themeKey, theme)
}

const userInfoKey = 'userInfo'

export function getUserInfo() {
	return localStorageGet(userInfoKey)
}

export function setUserInfo(userInfo) {
	localStorageSet(userInfoKey, userInfo)
}

const deviceNavigationComponentKey = 'deviceNavigationComponent'

export function getDeviceNavigationComponent() {
	return localStorageGet(deviceNavigationComponentKey) || 'deviceInfo'
}

export function setDeviceNavigationComponent(deviceNavigationComponent) {
	localStorageSet(deviceNavigationComponentKey, deviceNavigationComponent)
}

const statusBarKey = 'statusBar'

export function getStatusBar() {
	return localStorageGet(statusBarKey)
}

export function setStatusBar(statusBar) {
	localStorageSet(statusBarKey, statusBar)
}

const statusIndexKey = 'statusIndex'

export function getStatusIndex() {
	return localStorageGet(statusIndexKey) || 0
}

export function setStatusIndex(statusIndex) {
	localStorageSet(statusIndexKey, statusIndex)
}

const deviceTypeIndexKey = 'deviceTypeIndex'

export function getDeviceTypeIndex() {
	return localStorageGet(deviceTypeIndexKey) || 0
}

export function setDeviceTypeIndex(deviceTypeIndex) {
	localStorageSet(deviceTypeIndexKey, deviceTypeIndex)
}

const bottomBarHeightKey = 'bottomBarHeight'

export function getBottomBarHeight() {
	return localStorageGet(bottomBarHeightKey)
}

export function setBottomBarHeight(bottomBarHeight) {
	localStorageSet(bottomBarHeightKey, bottomBarHeight)
}

const mapHeightKey = 'mapHeight'

export function getMapHeight() {
	return localStorageGet(mapHeightKey)
}

export function setMapHeight(mapHeight) {
	localStorageSet(mapHeightKey, mapHeight)
}

const listModelKey = 'listModel'

export function getListModel() {
	return localStorageGet(listModelKey) || 'list'
}

export function setListModel(listModel) {
	localStorageSet(listModelKey, listModel)
}

const searchDeviceNameKey = 'searchDeviceName'

export function getSearchDeviceName() {
	return getFromNet(searchDeviceNameKey)
}

export function setSearchDeviceName(searchDeviceName) {
	return saveToNet(searchDeviceNameKey, searchDeviceName)
}

const statusBarHeightKey = 'statusBarHeight'

export function getStatusBarHeight() {
	return localStorageGet(statusBarHeightKey)
}

export function setStatusBarHeight(statusBarHeight) {
	localStorageSet(statusBarHeightKey, statusBarHeight)
}

const customBarHeightKey = 'customBarHeight'

export function getCustomBarHeight() {
	return localStorageGet(customBarHeightKey)
}

export function setCustomBarHeight(customBarHeight) {
	localStorageSet(customBarHeightKey, customBarHeight)
}

const screenWidthKey = 'screenWidth'

export function getScreenWidth() {
	return localStorageGet(screenWidthKey)
}

export function setScreenWidth(screenWidth) {
	localStorageSet(screenWidthKey, screenWidth)
}

const screenHeightKey = 'screenHeight'

export function getScreenHeight() {
	return localStorageGet(screenHeightKey)
}

export function setScreenHeight(screenHeight) {
	localStorageSet(screenHeightKey, screenHeight)
}

const durationKey = 'duration'

export function getDuration() {
	return localStorageGet(durationKey)
}

export function setDuration(duration) {
	localStorageSet(durationKey, duration)
}

const webSocketUrlKey = 'webSocketUrl'

export function getWebSocketUrl() {
	return getFromNet(webSocketUrlKey)
}

export function setWebSocketUrl(webSocketUrl) {
	return saveToNet(webSocketUrlKey, webSocketUrl)
}

const permissionsKey = 'permissions'

export function getPermissions() {
	return localStorageGet(permissionsKey)
}

export function setPermissions(permissions) {
	localStorageSet(permissionsKey, permissions)
}

